package com.sangmo.fts.process.receivable.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.common.utils.RandomUtils;
import com.sangmo.fts.process.receivable.interfaces.RpfPledgeContractCreation;
import com.sangmo.fts.process.receivable.model.RpfContract;
import com.sangmo.fts.process.receivable.model.RpfLoan;
import com.sangmo.fts.process.receivable.model.RpfPledgeContract;
import com.sangmo.fts.process.receivable.model.RpfWaybill;
import com.sangmo.fts.process.receivable.repo.RpfPledgeContractRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RpfPledgeContractEntityService extends AuthorityEntityService<RpfPledgeContract> {

    @Autowired
    private RpfPledgeContractRepo rpfPledgeContractRepo;

    @Autowired
    private RpfWaybillService rpfWaybillService;

    @Autowired
    private RpfContractService rpfContractService;

    @Autowired
    private RpfLoanService rpfLoanService;

    @Autowired
    private FileUploadService fileUploadService;

    @Override
    protected IDataViewRepo<RpfPledgeContract> getRepo() {
        return rpfPledgeContractRepo;
    }

    @Override
    protected void onCreate(RpfPledgeContract entity, Object bo) {
        super.onCreate(entity, bo);
        entity.setCode(RandomUtils.generateRandomNum(12));
        RpfPledgeContractCreation creation = (RpfPledgeContractCreation) bo;
        RpfWaybill waybill = rpfWaybillService.get(creation.getWaybillId());
        entity.setContractId(waybill.getContractId());
        RpfContract contract = rpfContractService.get(waybill.getContractId());
        entity.setPledgor(waybill.getShipper());
        RpfLoan loan = rpfLoanService.findByWaybillId(creation.getWaybillId());
        entity.setLoanId(loan.getId());
        entity.setPledgee(loan.getBankId());
        entity.setReceivableAmount(loan.getReceivableAmount());
        entity.setSubDebtor(loan.getSubDebtor());
        entity.setDueDate(contract.getPayDate());
    }

    @Override
    protected void afterCreated(RpfPledgeContract entity, Object bo) {
        super.afterCreated(entity, bo);
        RpfPledgeContractCreation creation = (RpfPledgeContractCreation) bo;
        if (null != creation.getPledgorConfirmDoc()) {
            fileUploadService.updateRef(creation.getPledgorConfirmDoc(), ""+entity.getId(), "RpfPledgeContract.Pledgor");
        }
    }

    @Override
    protected void onDelete(RpfPledgeContract entity) {
        super.onDelete(entity);
        if (entity.isPledgorConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.remove.permit.disabled");
        }
    }

    public void pledgorConfirm(RpfPledgeContract contract) {
        contract.setPledgorConfirm(true);
        this.save(contract);
    }

    public void pledgeeConfirm(RpfPledgeContract contract) {
        contract.setPledgeeConfirm(true);
        this.save(contract);
    }

    public void buyerConfirm(RpfPledgeContract contract) {
        contract.setBuyerConfirm(true);
        this.save(contract);
    }
}
